<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class TextareaCacheComponent extends Component
{

    public $_cache_config = 'textareas';
    private $_update_db_time = 6000;//下次更新时间（秒）
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 获取大文本信息缓存
     */
    public function getVal($content_type, $content_id)
    {
        $key = $content_type . '_' . $content_id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($content_type, $content_id) : $data;
    }

    /**
     * 创建大文本信息缓存
     */
    public function createCache($content_type, $content_id)
    {
        $key = $content_type . '_' . $content_id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Textareas');
        $data = $controller->Textareas->find()
            ->where(['content_type' => $content_type, 'content_id'=>$content_id])
            ->first();
        $cache = empty($data) ? null : ['id'=>$data['id'], 'content'=>$data['content']];
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 修改大文本信息缓存
     */
    public function update($content_type, $content_id, $content)
    {
        $key = $content_type . '_' . $content_id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Textareas');
        $data = $this->getVal($content_type, $content_id);
        if(empty($data)){
            return 'content_not_found';
        }else{
            $controller->Textareas->query()
                ->update()
                ->set(['content'=>$content])
                ->where(['id' => $data['id']])
                ->execute();
            $data['content'] = $content;
            Cache::write($key, $data, $this->_cache_config);
        }
        $this->deleteCache($content_type, $content_id);
        return true;
    }


    /**
     * 删除大文本信息
     */
    public function delete($content_type, $content_id)
    {
        $controller = $this->_registry->getController();
        $controller->loadModel('Textareas');
        $data = $this->getVal($content_type, $content_id);
        if(empty($data)){
            return 'content_not_found';
        }else{
//            $controller->Textareas->deleteAll(['id' => $data['id']]);
            $this->deleteCache($content_type, $content_id);
        }
        return true;
    }

    /**
     * 删除大文本信息缓存
     */
    public function deleteCache($content_type, $content_id)
    {
        $key = $content_type . '_' . $content_id;
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /**
     * 添加大文本信息
     */
    public function add($content_type, $content_id, $content)
    {
        $controller = $this->_registry->getController();
        $controller->loadModel('Textareas');
        $textarea = $controller->Textareas->newEntity();
        $textarea->content_type = $content_type;
        $textarea->content_id = $content_id;
        $textarea->content = $content;
        $controller->Textareas->save($textarea);
        return true;
    }
}